package work.parabola.keyboardswitch.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import work.parabola.keyboardswitch.common.ResponseCode;
import work.parabola.keyboardswitch.common.ServerResponse;
import work.parabola.keyboardswitch.dao.AuthorDoMapper;
import work.parabola.keyboardswitch.pojo.query.author.ListQuery;
import work.parabola.keyboardswitch.pojo.repository.AuthorDo;
import work.parabola.keyboardswitch.pojo.vo.AuthorListVo;
import work.parabola.keyboardswitch.service.IAuthorService;

import java.util.ArrayList;
import java.util.List;

@Service
public class AuthorServiceImpl implements IAuthorService {

    @Autowired
    private AuthorDoMapper authorDoMapper;

    @Override
    public ServerResponse getAuthorList(ListQuery listQuery) {
        if(listQuery == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),
                    ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        ServerResponse response = listQuery.validate();
        if(!response.isSuccess()){
            return response;
        }
        List<AuthorDo> authorDoList = this.authorDoMapper.selectByParam(listQuery.getSearchWord());
        List<AuthorListVo> authorListVoList = new ArrayList<>();
        for(AuthorDo authorDo: authorDoList){
            AuthorListVo vo = new AuthorListVo();
            vo.setId(authorDo.getId());
            vo.setName(authorDo.getName());
            authorListVoList.add(vo);
        }
        return ServerResponse.createBySuccess(authorListVoList);
    }
}
